﻿namespace _01_Tower2._0
{
    internal class ToolMenu
    {
        struct InfoData
        {
            public string str;
            public ConsoleColor color;
        }

        public enum ToolSelected
        {
            none,
            tool1,
            tool2,
            tool3,
            tool4,
            tool5,
            tool6,
            tool7,
            tool8,
            tool9,
            tool10
        }

        InfoData[] infoList;
        int index;//指针
        int maxCount;//最大存储量
        int offsetX, offsetY;
        ConsoleKeyInfo key;
        public bool isOpen;
        long lastTime, currentTime;
        int delay;
        int row, col;
        public ToolSelected toolSelected;

        public ToolMenu(int x, int y, int row, int col, int delay) 
        {
            index = 0;
            maxCount = 10;
            infoList = new InfoData[maxCount];
            offsetX = x;
            offsetY = y;
            isOpen = false;
            this.delay = delay * 10000;
            this.row = row;
            this.col = col;
            lastTime = DateTime.Now.Ticks;
            toolSelected = ToolSelected.none;
        }

        

        public void AddInfo(string str, ConsoleColor color)
        {
            if (index > maxCount - 1)
            {
                index = maxCount - 1;
                //FowardInfoArray();
            }
            InfoData info = new InfoData();
            info.str = str;
            info.color = color;
            infoList[index++] = info;
        }

        void FowardInfoArray()
        {
            for (int i = 0; i < maxCount - 1; i++)
            {
                infoList[i] = infoList[i + 1];
            }
        }

        void PrintFrame()
        {
            for (int i = 0; i < col; i++)
            {
                MyPrint("  ", offsetX + i, offsetY + row);
            }
            for (int i = 0; i < row; i++)
            {
                for (int j = 0; j < col; j++)
                {
                    string temp = "";
                    if (i == 0 && j == 0)
                    {
                        /*MyPrint("┌", offsetX + j, offsetY + i);*/
                        temp = "┌";
                    }
                    else if (i == 0 && j == col - 1)
                    {
                        /*MyPrint("┐", offsetX + j, offsetY + i);*/
                        temp = "┐";
                    }
                    else if (i == row - 1 && j == 0)
                    {
                        /*MyPrint("└", offsetX + j, offsetY + i);*/
                        temp = "└";
                    }
                    else if (i == row - 1 && j == col - 1)
                    {
                        /*MyPrint("┘", offsetX + j, offsetY + i);*/
                        temp = "┘";
                    }
                    else if (i == 0 || i == row - 1)
                    {
                        /* MyPrint("一", offsetX + j, offsetY + i);*/
                        temp = "─";
                    }
                    else if (j == 0 || j == col - 1)
                    {
                        /* MyPrint("|", offsetX + j, offsetY + i);*/
                        temp = "│";
                    }
                    else
                    {
                        temp = "  ";
                    }
                    MyPrint(temp, offsetX + j, offsetY + i);
                }
            }
        }

        void PrinText()
        {
            for (int i = 0; i < row - 2; i++)
            {
                MyPrint(infoList[i].str, offsetX + 3, offsetY + 1 + i, infoList[i].color);
            }
        }

        public void PrintInfo()
        {
            PrintFrame();
            PrinText();
            if(isOpen == true)
            {
                ShowToolSelect();
            }
        }

        void MyPrint(string str, int x, int y, ConsoleColor color = ConsoleColor.White)
        {
            Console.SetCursorPosition(x, y);
            Console.ForegroundColor = color;
            Console.Write(str);
            Console.ResetColor();
        }

        public void Input()
        {
            if (InputSystem.Instance.IsKeyPressed(ConsoleKey.S))
            {
                toolSelected++;
                if ((int)toolSelected > 10)
                {
                    toolSelected = ToolSelected.tool1;
                }
            }
            if (InputSystem.Instance.IsKeyPressed(ConsoleKey.W))
            {
                toolSelected--;
                if ((int)toolSelected < 1)
                {
                    toolSelected = ToolSelected.tool10;
                }
            }
        }

        void ShowToolSelect()
        {
            if (toolSelected == ToolSelected.tool1)
            {
                Utility.MyPrint(">", 85, 13); Utility.MyPrint("<", 97, 13);
            }
            else
            {
                Utility.MyPrint(" ", 85, 13); Utility.MyPrint(" ", 97, 13);
            }
            if (toolSelected == ToolSelected.tool2)
            {
                Utility.MyPrint(">", 85, 14); Utility.MyPrint("<", 97, 14);
            }
            else
            {
                Utility.MyPrint(" ", 85, 14); Utility.MyPrint(" ", 97, 14);
            }
            if (toolSelected == ToolSelected.tool3)
            {
                Utility.MyPrint(">", 85, 15); Utility.MyPrint("<", 97, 15);
            }
            else
            {
                Utility.MyPrint(" ", 85, 15); Utility.MyPrint(" ", 97, 15);
            }
            if (toolSelected == ToolSelected.tool4)
            {
                Utility.MyPrint(">", 85, 16); Utility.MyPrint("<", 97, 16);
            }
            else
            {
                Utility.MyPrint(" ", 85, 16); Utility.MyPrint(" ", 97, 16);
            }
            if (toolSelected == ToolSelected.tool5)
            {
                Utility.MyPrint(">", 85, 17); Utility.MyPrint("<", 97, 17);
            }
            else
            {
                Utility.MyPrint(" ", 85, 17); Utility.MyPrint(" ", 97, 17);
            }
            if (toolSelected == ToolSelected.tool6)
            {
                Utility.MyPrint(">", 85, 18); Utility.MyPrint("<", 97, 18);
            }
            else
            {
                Utility.MyPrint(" ", 85, 18); Utility.MyPrint(" ", 97, 18);
            }
            if (toolSelected == ToolSelected.tool7)
            {
                Utility.MyPrint(">", 85, 19); Utility.MyPrint("<", 97, 19);
            }
            else
            {
                Utility.MyPrint(" ", 85, 19); Utility.MyPrint(" ", 97, 19);
            }
            if (toolSelected == ToolSelected.tool8)
            {
                Utility.MyPrint(">", 85, 20); Utility.MyPrint("<", 97, 20);
            }
            else
            {
                Utility.MyPrint(" ", 85, 20); Utility.MyPrint(" ", 97, 20);
            }
            if (toolSelected == ToolSelected.tool9)
            {
                Utility.MyPrint(">", 85, 21); Utility.MyPrint("<", 97, 21);
            }
            else
            {
                Utility.MyPrint(" ", 85, 21); Utility.MyPrint(" ", 97, 21);
            }
            if (toolSelected == ToolSelected.tool10)
            {
                Utility.MyPrint(">", 85, 22); Utility.MyPrint("<", 97, 22);
            }
            else
            {
                Utility.MyPrint(" ", 85, 22); Utility.MyPrint(" ", 97, 22);
            }
        }

        public void Update()
        {
            currentTime = DateTime.Now.Ticks;
            if (currentTime - lastTime > delay)
            {
                if (isOpen)
                {
                    row++;
                    if (row > 12) row = 12;
                }
                else
                {
                    row--;
                    if (row < 0) row = 0;
                }
                lastTime = DateTime.Now.Ticks;
            }

        }
    }
}
